<script lang="ts" setup>
import PageDialog from "@/components/PageDialog/index.vue";
import {ref} from "vue";
import {OwnerObj, ProjectObj} from "@/api/demoClass";
import {addOwner, addProject, editOwner, editProject} from "@/api/demoApi";
import {message} from "@/utils/message";

defineOptions({
  name: "ownerDialog"
});

/**-------------------表单相关-------------------**/
//表单参数
const formData = ref({
  id: null,
  ownerName: null,
  contact: null,
  phone: null,
  taxNumber: null,
  address: null,
  paymentCount: 0
})

//表单校验
const formRules = ref({
  ownerName: [{required:true,message:'请输入业主名称',trigger:['blur','change']}],
})

/**-------------------打开弹框-------------------**/
//标题
const dialogTitle = ref('')
const dialog = ref()
//弹框显示
const dialogShow = (title: string,data?: object)=>{
  dialogTitle.value = title
  if(data) {
    formData.value = {...data}
  }
  dialog.value.dialogVisible = true
}
defineExpose({
  dialogShow
})

/**-------------------事件回调-------------------**/
//取消
const form = ref()
const handlerCancel = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.resetFields()
    formData.value = {
      id: null,
      ownerName: null,
      contact: null,
      phone: null,
      taxNumber: null,
      address: null,
      paymentCount: 0
    }
    dialog.value.dialogVisible = false
    resolve({})
  }catch(e){
    console.log(e)
    reject(e)
  }
})
//确认
const emits = defineEmits(['done'])
const handlerConfirm = ()=> new Promise(async (resolve,reject)=>{
  try{
    await form.value.validate()
    //todo 新增、编辑接口
    if(!formData.value.id) {
      const newProject = new OwnerObj(formData.value)
      addOwner(newProject)
      message("添加成功", { type: "success" });
    }else {
      editOwner(formData.value)
      message("编辑成功", { type: "success" });
    }
    await handlerCancel()
    emits('done')
    resolve({})
  }catch(e){
    reject(e)
  }
})
</script>

<template>
  <PageDialog ref="dialog" :handler-cancel="handlerCancel" :handler-confirm="handlerConfirm" :title="dialogTitle">
    <el-form ref="form" :model="formData" :rules="formRules" label-position="top" label-width="auto">
      <el-form-item label="业主名称" prop="ownerName">
        <el-input v-model="formData.ownerName" clearable placeholder="请输入业主名称" />
      </el-form-item>
      <el-form-item label="业主联系人" prop="contact">
        <el-input v-model="formData.contact" clearable placeholder="请输入业主联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="formData.phone" clearable placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="业主地址" prop="address">
        <el-input v-model="formData.address" clearable placeholder="请输入业主地址" />
      </el-form-item>
      <el-form-item label="税号/信用代码" prop="taxNumber">
        <el-input v-model="formData.taxNumber" clearable placeholder="请输入税号/信用代码" />
      </el-form-item>
    </el-form>
  </PageDialog>
</template>

<style lang="scss" scoped>
//:deep(.el-form) {
//  display: flex;
//  justify-content: space-between;
//  align-items: center;
//  flex-wrap: wrap;
//
//  .el-form-item {
//    width: 48%;
//
//    .el-input,
//    .el-select {
//      width: 100%;
//    }
//  }
//}
</style>
